home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1990 / number3 / setcrc.c < prev    next >
C/C++ Source or Header  |  1990-04-15  |  5KB  |  158 lines

  1. /*************************************************************************
  2.  *  Listing 2 - SETCRC.C
  3.  *
  4.  *  This program is used to initialize the valid CRC in application
  5.  *  programs incorporating the anti_venom routine.
  6.  *
  7.  *  Written by Kevin D. Weeks 10-8-89
  8.  *  Released to the Public Domain.
  9.  */
  10. #include <stdio.h>
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15.  
  16. #define BUF_SIZE    1024                /* size of file read buffer */
  17. #define ERR         -1
  18. #define FOUND       10
  19.  
  20. /* function prototypes */
  21. void    usage(char *);
  22. void    sig_to_bin(char *,char *);
  23.  
  24. main(int argc,char *argv[])
  25. {
  26.     int             handle;             /* file handle */
  27.     unsigned int    i, j;               /* loop counters */
  28.     char            *buffer;            /* pointer to file buffer */
  29.     char            signature[10];      /* crc signature */
  30.     unsigned int    num_bytes;      /* number of bytes read from file */
  31.     int             state;              /* current machine state */
  32.     unsigned int    cur_crc;            /* running crc */
  33.     long            crc_location;       /* file location of valid crc */
  34.     int             finds;          /* number of times signature found */
  35.  
  36.     if (argc != 3)
  37.         usage("\aParameter missing.");
  38.     if ((buffer = (char *)malloc(BUF_SIZE)) == NULL)
  39.     {
  40.         printf("\aError allocating memory.\n");
  41.         exit(1);
  42.     }
  43.     /* convert the signature to binary values */
  44.     sig_to_bin(signature,argv[2]);
  45.     if ((handle = open(argv[1],O_BINARY | O_RDWR)) == ERR)
  46.     {
  47.         perror(argv[1]);
  48.         exit(1);
  49.     }
  50.     /* initialize everything */
  51.     lseek(handle,0L,0);
  52.     state = 0;
  53.     cur_crc = 0;
  54.     finds = 0;
  55.     crc_location = 0L;
  56.  
  57.     /* loop until num_bytes == 0 (EOF) */
  58.     while (num_bytes = read(handle,buffer,BUF_SIZE))
  59.     {
  60.         for (i = 0; i < num_bytes; i++)
  61.         {
  62.             /* first calculate the crc */
  63.             /* xor current byte with current crc's hi-byte */
  64.             cur_crc ^= (unsigned int)buffer[i] << 8;
  65.             /* devide current crc, modulo 2, by prime polynomial */
  66.             for (j = 0; j < 8; j++)
  67.                 if (cur_crc & 0x8000)           /* if MSB on */
  68.                     /* shift left and xor with prime */
  69.                     cur_crc = (cur_crc << 1) ^ 0x1021;
  70.                 else                            /* otherwise */
  71.                     cur_crc <<= 1;              /* just shift left */
  72.  
  73.             /* then search for the signature */
  74.             if (state != FOUND)
  75.                 if (buffer[i] == signature[state])
  76.                 {
  77.                     ++state;
  78.                     if (signature[state] == 0)
  79.                     {
  80.                         state = FOUND;
  81.                         ++i;            /* point to crc position */
  82.                         crc_location += i;
  83.                         ++i;          /* skip over crc position */
  84.                         ++finds;      /* increment number of times found */
  85.                     }
  86.                 }
  87.                 else
  88.                     state = 0;
  89.         }
  90.         if (state != FOUND)
  91.             crc_location += (long)num_bytes;  /* update the file pointer */
  92.     }
  93.     if (finds == 1)
  94.     {
  95.         printf("crc = %xh @ file offset %lxh\n",cur_crc,crc_location);
  96.         lseek(handle,crc_location,0);
  97.         write(handle,&cur_crc,2);
  98.     }
  99.     else
  100.         if (finds == 0)
  101.             printf("\aError: Signature not found in %s.\n",argv[1]);
  102.         else
  103.             printf("\aError: Signature found more than once.\n");
  104.     close(handle);
  105.     exit(0);
  106. }
  107.  
  108. /*************************************************************************
  109.  *  This function converts the hexadecimal signature passed on the command
  110.  *  line to the binary values that actually have to be searched for.
  111.  */
  112. void    sig_to_bin(char *signature,char *argv)
  113. {
  114.     int     i, j;
  115.  
  116.     /* convert signature to binary values */
  117.     j = i = 0;
  118.     signature[j] = 0;
  119.     while (argv[i])
  120.     {
  121.         if (argv[i] != ',')
  122.         {
  123.             signature[j] *= 16;
  124.             if (argv[i] > 47 && argv[i] < 58)         /* 0 - 9 */
  125.                 signature[j] += argv[i] - 48;
  126.             else
  127.                 if (argv[i] > 40 && argv[i] < 71)     /* A - F */
  128.                     signature[j] += argv[i] - 55;
  129.                 else
  130.                     if (argv[i] > 96 && argv[i] < 103) /* a - f */
  131.                         signature[j] += argv[i] - 87;
  132.                     else
  133.                         usage("\aInvalid hex value in signature");
  134.         }
  135.         else
  136.         {
  137.             if (j == 8)
  138.                 break;
  139.             signature[++j] = 0;
  140.         }
  141.         ++i;
  142.     }
  143.     signature[++j] = 0;
  144. }
  145.  
  146. /*************************************************************************
  147.  *  Usage prints error messages and a short help message.
  148.  *
  149.  */
  150. void    usage(char *message)
  151. {
  152.     printf("\nSETCRC, Rev 1.0    %s\n\n",message);
  153.     printf("Usage: SETCRC FILENAME SIGNATURE      ");
  154.     printf("(Note: signature must be in hex)\n");
  155.     printf("Example: setcrc testprog.exe 41,4e,54,49,56\n");
  156.     exit(1);
  157. }
  158.